<?php
require_once("../../inc/global.php");
require_once("function.php");
require_once("fun.php");
adminislogin();
/*
if($_SESSION['isinsertdata']==false){
	exit('您的网站存储空间已不足，已无法添加数据，请及时与青弥互动联系!');
}
*/

$MenuID=$_GET["MenuID"];
if(empty($MenuID)||!is_numeric($MenuID)){
	YKT_error("参数不正确",$_SERVER['HTTP_REFERER']);
}

if(!isGetRule($MenuID,$_SESSION[ADMINSESSION][0],2) && strtolower($_SESSION[ADMINSESSION][1])!='admin'){
	YKT_error("对不起，你没有操作的权限",$_SERVER['HTTP_REFERER']);
}

$html->includetop();
$GetTableName=$System->GetTableName($MenuID);  //获取表名
$getfromconfig=$System->GetMenuSysFormConfig($MenuID);  //获取配置
$getfromconfigarr=explode(",",$getfromconfig);


//创建添加sql
function getfieldtype($TableName,$GetFileNum){
	$db=new mysql();
	$result=$db->query("select * from {$TableName} limit 1");
	return @mysql_field_type($result,$GetFileNum);
}


//把获取过来的数据存入数组中
$j=0;
$isupvalue=0;
for($p=1;$p<count($getfromconfigarr);$p++){
	$getfromconfiglist=explode("|",$getfromconfigarr[$p]);
	if(intval($getfromconfiglist[4])==0){
			if($getfromconfiglist[1]=="6"){  //是否为上传图片提交的数据
				
				$getupfile=upfilepic("FileName_{$p}");
				
				if(!is_array($getupfile)){
					$postarr[$j]["fieldvalue"]=$getupfile;
				}else{
					if(strval($getupfile[1])=="无效的文件"){
						//$isupvalue=1;
						$postarr[$j]["fieldvalue"]="";
					}else{
						//$postarr[$j]["fieldvalue"]=$_POST["FileName_{$p}_path"];
					}
					//$base->alert($getupfile[1]);
				}
			}elseif($getfromconfiglist[1]=="16"){
				$getupfile=Uploadfiles("FileName_{$p}",$getfromconfiglist[5]);
				if(!is_array($getupfile)){
					$postarr[$j]["fieldvalue"]=$getupfile;
				}else{
					if(strval($getupfile[1])=="无效的文件"){
						//$isupvalue=1;
						$postarr[$j]["fieldvalue"]="";
					}else{
						//$postarr[$j]["fieldvalue"]=$_POST["FileName_{$p}_path"];
					}
					//$base->alert($getupfile[1]);
				}
				
				
				
				
			}elseif($getfromconfiglist[1]=="4"){
				
				$postcheckvalue=isset($_POST["FileName_{$p}"])?$_POST["FileName_{$p}"]:'';

				if(is_array($postcheckvalue))
				{
					$checkvalue="";
					for($c=0;$c<count($postcheckvalue);$c++){
						$checkvalue.=$postcheckvalue[$c];
						if($c<count($postcheckvalue)-1){
							$checkvalue.=",";
						}
					}
					$postarr[$j]["fieldvalue"]=addslashes($checkvalue);
				}else{
					if(isset($_POST["FileName_{$p}"])){
						$postarr[$j]["fieldvalue"]=isset($_POST["FileName_{$p}"])?addslashes($_POST["FileName_{$p}"]):'';
					}
				}
				
			}elseif($getfromconfiglist[1]=="10"){
				
				if(isset($_POST["FileName_{$p}"])){
					if(empty($_POST["FileName_{$p}"])){
						$postarr[$j]["fieldvalue"]=time();
					}else{
						$postarr[$j]["fieldvalue"]=strtotime($_POST["FileName_{$p}"]);
					}
				}

			}else{
				if(isset($_POST["FileName_{$p}"])){
					$postarr[$j]["fieldvalue"]=addslashes($_POST["FileName_{$p}"]);
				}
			}
			
			
			if($isupvalue==0){
				$postarr[$j]["fieldname"]=$GetTableName.$p;
				if(strtolower(getfieldtype($GetTableName,$p))=="int"){
					$postarr[$j]["type"]="int";
				}elseif(strtolower(getfieldtype($GetTableName,$p))=="date"||strtolower(getfieldtype($GetTableName,$p))=="datetime"||strtolower(getfieldtype($GetTableName,$p))=="time"){
					$postarr[$j]["type"]="datetime";
				}else{
					$postarr[$j]["type"]="string";
				}
			}
			$isupvalue=0;
			$j++;
			
			
	}
}

$addSql="insert into {$GetTableName}";
$addfield="(";
$addvalue="values(";
$dian=0;

for($i=0;$i<count($postarr);$i++){
	if($postarr[$i]["type"]=="int"){
		$addfield.="{$postarr[$i]["fieldname"]}";
		$addvalue.="{$postarr[$i]["fieldvalue"]}";
		if($i<(count($postarr)-1)){
			$addfield.=",";
			$addvalue.=",";
		}
	}elseif($postarr[$i]["type"]=="datetime"){
		if($postarr[$i]["fieldvalue"]!=""){
			$addfield.="{$postarr[$i]["fieldname"]}";
			$addvalue.="'{$postarr[$i]["fieldvalue"]}'";
			if($i<(count($postarr)-1)){
				$addfield.=",";
				$addvalue.=",";
			}
		}else{
			$addfield.="{$postarr[$i]["fieldname"]}";
			$addvalue.="null";
			if($i<(count($postarr)-1)){
				$addfield.=",";
				$addvalue.=",";
			}
		}
	}else{
		if(isset($postarr[$i]["fieldvalue"])){
			$addfield.="{$postarr[$i]["fieldname"]}";
			$addvalue.="'{$postarr[$i]["fieldvalue"]}'";
			if($i<(count($postarr)-1)){
				$addfield.=",";
				$addvalue.=",";
			}
		}
	}
}
$addfield.=")";
$addvalue.=")";
$addSql.=$addfield.$addvalue;
if(isset($_GET["SuperAdd"])){
	$SuperAdd=$_GET["SuperAdd"];
}else{
	$SuperAdd=0;
}
if($SuperAdd==1){
	$SuperNum=$_POST["SuperNum"];
	if(is_numeric($SuperNum)){
		for($i=1;$i<=$SuperNum;$i++){
			if(!$db->query($addSql)){
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据批量添加失败","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
			}
		}
		YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据批量添加成功","/".ADMINROOT."/System/onepagesys/index.php?MenuID={$MenuID}");
	}else{
		if($db->query($addSql)){
			if(strtolower($GetTableName)=="onepagesys"){
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功","/".ADMINROOT."/System/onepagesys/index.php?MenuID={$MenuID}");
			}else{
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
			}
		}else{
			if(strtolower($GetTableName)=="onepagesys"){
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加失败","/".ADMINROOT."/System/onepagesys/index.php?MenuID={$MenuID}");
			}else{
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加失败","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
			}
		}
	}
}else{
	if($db->query($addSql)){
		$insertid = $db->getinsertid();
		$mm=$db->getoners('select * from menusys where menusys0='.$MenuID);
		if(strtolower($GetTableName)=="onepagesys"){
			if(ISCREATEHTML==true && ((!empty($mm['menusys13']) && !empty($mm['menusys19'])) || (!empty($mm['mmenusys13']) && !empty($mm['mmenusys19'])))){
				$url='http://'.$_SERVER['HTTP_HOST'].'/'.ADMINROOT.'/System/htmlsys/index.php?type=type1&id='.$MenuID;
				$status=get_headers($url);
				if(strpos($status[0],'200')){
					YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功,静态页面生成成功！","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
				}else{
					YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功,静态页面生成失败！","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
				}
			}else{
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功","/".ADMINROOT."/System/onepagesys/index.php?MenuID={$MenuID}");
			}
		}else{
			$s=$db->query('update menusys set menusys21="'.time().'" where menusys0='.$MenuID. ' limit 1');
			if(ISCREATEHTML==true && ((!empty($mm['menusys16']) && !empty($mm['menusys17'])) || (!empty($mm['mmenusys16']) && !empty($mm['mmenusys17'])))){
				$thisid=$insertid;
				$url='http://'.$_SERVER['HTTP_HOST'].'/'.ADMINROOT.'/System/htmlsys/index.php?type=type3&id='.$MenuID.'&contentid='.$thisid;
				$status=get_headers($url);
				if(strpos($status[0],'200')){
					YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功,静态页面生成成功！","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
				}else{
					YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功,静态页面生成失败！","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
				}
			}else{
				YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加成功","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");	
			}
			
		}
	}else{
		if(strtolower($GetTableName)=="onepagesys"){
			YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加失败","/".ADMINROOT."/System/onepagesys/index.php?MenuID={$MenuID}");
		}else{
			YKT_error("[".$System->GetMenuSysField($MenuID,1)."] 数据添加失败","/".ADMINROOT."/System/{$GetTableName}/index.php?MenuID={$MenuID}");
		}
	}
}
$html->includeend();
?>